//
//  CaiPuRenQiViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CaiPuRenQiViewController.h"
#import "FitHeader.h"
#import "CaiPuRenQiColumnCollectionViewCell.h"
#import "CaiPuRenQiContentCollectionViewCell.h"
#import "ZJHShakeCaipuViewController.h"
#import "ZJHShakeModel.h"
@interface CaiPuRenQiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, CaiPuRenQiTableViewSelectDelegate>
@property(nonatomic, strong)UICollectionView *columnCollection;
@property(nonatomic, strong)UICollectionView *contentCollection;
@property(nonatomic, strong)NSMutableArray *columnArray;
@property(nonatomic, strong)NSMutableArray *columnTypeArray;
@property(nonatomic, strong)NSMutableArray *contentArray;
@property(nonatomic, assign)NSInteger indexFlag;
@end

@implementation CaiPuRenQiViewController

#pragma mark 人气菜谱跳转菜谱介绍界面方法实现
-(void)pushCaiPuContentViewWithReceipId:(NSInteger)receipId Title:(NSString *)title
{
    ZJHShakeCaipuViewController *caipuVC = [[ZJHShakeCaipuViewController alloc]init];
    ZJHShakeModel *model = [[ZJHShakeModel alloc]init];
    model.RecipeId = receipId;
    model.Title = title;
    caipuVC.tempModel = model;
    [self.navigationController pushViewController:caipuVC animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"人气食谱";
}
-(void)loadView
{
    [super loadView];
    _indexFlag = 0;
    [self createUI];
    
}

-(void)createUI
{
    //栏目条collection
    //栏目条数组
    _columnArray = [NSMutableArray arrayWithObjects:@"全部", @"私人", @"时令", @"达人", @"最新", @"烘焙", nil];
    _columnTypeArray = [NSMutableArray arrayWithObjects:@"热门菜谱", @"私人定制", @"时令佳肴", @"达人菜谱", @"最新菜谱", @"快乐的烘焙", nil];
    UICollectionViewFlowLayout *columnFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    columnFlowLayout.itemSize = CGSizeMake(40 * wt, 35 * ht);
    columnFlowLayout.minimumInteritemSpacing = 0;
    columnFlowLayout.minimumLineSpacing = 20 * wt;
    columnFlowLayout.sectionInset = UIEdgeInsetsMake(10 * wt, 10 * ht, 0, 10 * ht);
    columnFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _columnCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 45 * ht) collectionViewLayout:columnFlowLayout];
    _columnCollection.dataSource = self;
    _columnCollection.delegate = self;
    _columnCollection.backgroundColor = [UIColor whiteColor];
    [_columnCollection registerClass:[CaiPuRenQiColumnCollectionViewCell class] forCellWithReuseIdentifier:@"columnIdentifier"];
    [self.view addSubview:_columnCollection];
    
    //详细内容collection
    UICollectionViewFlowLayout *contentFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    contentFlowLayout.itemSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT - 64 - 45 * ht);
    contentFlowLayout.minimumInteritemSpacing = 0;
    contentFlowLayout.minimumLineSpacing = 0;
    contentFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    contentFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _contentCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 45 * ht, SCREENWIDTH, SCREENHEIGHT - 64 - 45 * ht) collectionViewLayout:contentFlowLayout];
    _contentCollection.dataSource = self;
    _contentCollection.delegate = self;
    _contentCollection.bounces = NO;
    _contentCollection.pagingEnabled = YES;
    [_contentCollection registerClass:[CaiPuRenQiContentCollectionViewCell class] forCellWithReuseIdentifier:@"contentIdentifier"];
    [self.view addSubview:_contentCollection];
    self.automaticallyAdjustsScrollViewInsets = NO;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_columnCollection == collectionView) {
        CaiPuRenQiColumnCollectionViewCell *columnCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"columnIdentifier" forIndexPath:indexPath];
        columnCell.titleLable.text = _columnArray[indexPath.row];
        if (indexPath.row == _indexFlag) {
            columnCell.titleLable.textColor = [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
            columnCell.lineImageView.backgroundColor = [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
        }
        else
        {
            columnCell.titleLable.textColor = [UIColor blackColor];
            columnCell.lineImageView.backgroundColor = [UIColor whiteColor];
        }
        return columnCell;
    }
    else
    {
        CaiPuRenQiContentCollectionViewCell *contentCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"contentIdentifier" forIndexPath:indexPath];
        contentCell.delegate = self;
        contentCell.type = _columnTypeArray[indexPath.row];
        contentCell.title = _columnArray[indexPath.row];
        return contentCell;
    }
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_columnCollection == collectionView) {
        return _columnArray.count;
    }
    else
    {
        return _columnArray.count;
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _columnCollection) {
//        改变前一个cell颜色
        NSIndexPath *preIndexPath = [NSIndexPath indexPathForRow:_indexFlag inSection:0];
        CaiPuRenQiColumnCollectionViewCell *preColumnCell = (CaiPuRenQiColumnCollectionViewCell *)[collectionView cellForItemAtIndexPath:preIndexPath];
        preColumnCell.titleLable.textColor = [UIColor blackColor];
        preColumnCell.lineImageView.backgroundColor = [UIColor whiteColor];
        
//        改变当前cell颜色
        CaiPuRenQiColumnCollectionViewCell *columnCell = (CaiPuRenQiColumnCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
            [UIView animateWithDuration:.5 animations:^{
                columnCell.titleLable.textColor = [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
                columnCell.lineImageView.backgroundColor = [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
            }];
        
        _indexFlag = indexPath.row;
        [_contentCollection setContentOffset:CGPointMake(SCREENWIDTH * indexPath.row, 0)];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
        if (scrollView == _contentCollection) {
            
            //改变前一个cell颜色
            NSIndexPath *preIndexPath = [NSIndexPath indexPathForRow:_indexFlag inSection:0];
            CaiPuRenQiColumnCollectionViewCell *preColumnCell = (CaiPuRenQiColumnCollectionViewCell *)[_columnCollection cellForItemAtIndexPath:preIndexPath];
            preColumnCell.titleLable.textColor = [UIColor blackColor];
            preColumnCell.lineImageView.backgroundColor = [UIColor whiteColor];
    
    
            _indexFlag = scrollView.contentOffset.x / SCREENWIDTH;
            //改变当前cell颜色
            NSIndexPath *curIndexPath = [NSIndexPath indexPathForRow:_indexFlag inSection:0];
            CaiPuRenQiColumnCollectionViewCell *curColumnCell = (CaiPuRenQiColumnCollectionViewCell *)[_columnCollection cellForItemAtIndexPath:curIndexPath];
            [UIView animateWithDuration:.5 animations:^{
            curColumnCell.titleLable.textColor = [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
            curColumnCell.lineImageView.backgroundColor = [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
            }];
        }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
